package com.duleme.servlet;


import com.duleme.common.Config;
import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.dao.BookDao;
import com.duleme.dao.ShopDao;
import com.duleme.dao.UserDao;
import com.duleme.dao.UserInfoDao;
import com.duleme.entity.Book;
import com.duleme.entity.Shop;
import com.duleme.entity.User;
import com.duleme.entity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/update/admin")
public class AdminUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request, response);
        String method = request.getParameter("method");
        String target = request.getParameter("target");
        //更新模为修改
        if (method.equals("modify")) {
            if (target.equals("user")) {
                String userName = request.getParameter("userName");
                String avatar = request.getParameter("avatar");
                String userId = request.getParameter("userId");
                User user = new User();
                user.setUserId(Integer.parseInt(userId));
                user.setAvatar(avatar);
                user.setUserName(userName);
                UserDao dao = new UserDao();
                if (dao.update(user)) {
                    response.getWriter().print(JsonUtils.result2Json("1"));
                } else response.getWriter().print(JsonUtils.result2Json("0"));
            } else if (target.equals("userInfo")) {
                String birthday=request.getParameter("birthday");
                String sex=request.getParameter("sex");
                String phone=request.getParameter("phoneNumber");
                String email=request.getParameter("emailAddress");
                String userId=request.getParameter("userId");
                UserInfoDao dao=new UserInfoDao();
                UserInfo userInfo=new UserInfo();
                userInfo.setUserId(Integer.parseInt(userId));
                Date date=null;
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date=dateFormat.parse(birthday);
                }catch (ParseException e){
                    e.printStackTrace();
                    response.getWriter().print(JsonUtils.result2Json("0","info","日期数据格式错误"));
                }
                userInfo.setBirthday(date);
                userInfo.setSex(Integer.parseInt(sex));
                userInfo.setPhoneNumber(phone);
                userInfo.setEmailAddress(email);
                if(dao.updateUserInfo(userInfo))
                    response.getWriter().print(JsonUtils.result2Json("1"));
                else
                    response.getWriter().print(JsonUtils.result2Json("0"));
            }else if (target.equals("shop")){
                String shopAvatar=request.getParameter("shopAvatar");
                String shopName=request.getParameter("shopName");
                String shopId=request.getParameter("shopId");
                ShopDao dao=new ShopDao();
                if(dao.update(shopAvatar,shopName,Integer.parseInt(shopId))){
                    response.getWriter().print(JsonUtils.result2Json("1"));
                }else
                    response.getWriter().print(JsonUtils.result2Json("0"));
            }else if (target.equals("book")){
                String bookName=request.getParameter("bookName");
                String synopsis=request.getParameter("synopsis");
                String author=request.getParameter("author");
                String press=request.getParameter("press");
                Date date=null;
                String publishDate=request.getParameter("publishDate");
                try {
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    date=simpleDateFormat.parse(publishDate);
                }catch (ParseException e){
                    e.printStackTrace();
                    response.getWriter().print(JsonUtils.result2Json("0"));
                }
                System.out.println(date.getTime());
                String cover=request.getParameter("cover");
                String remain=request.getParameter("remain");
                String price=request.getParameter("price");
                String category=request.getParameter("category");
                int categoryId= Config.CATEGORY.get(category);
                String bookId=request.getParameter("bookId");
                Book book=new Book();
                book.setBookId(Integer.parseInt(bookId));
                book.setBookName(bookName);
                book.setSynopsis(synopsis);
                book.setAuthor(author);
                book.setPress(press);
                book.setPublishDate(date);
                book.setCover(cover);
                book.setRemain(Integer.parseInt(remain));
                book.setPrice(Double.parseDouble(price));
                book.setCategory(category);
                book.setCategoryId(categoryId);
                BookDao dao=new BookDao();
                if (dao.updateBookInfo(book))
                    response.getWriter().print(JsonUtils.result2Json("1"));
                else
                    response.getWriter().print(JsonUtils.result2Json("0"));
            }else
                response.getWriter().print(JsonUtils.result2Json("0"));
        }


        //更新模式为删除
        if (method.equals("delete")) {
            if (target.equals("user")) {
                String userId = request.getParameter("userId");
                UserDao dao = new UserDao();
                boolean result = false;
                try {
                    result = dao.deleteUserByUserId(Integer.parseInt(userId));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (result) {
                    response.getWriter().print(JsonUtils.result2Json("1"));
                } else response.getWriter().print(JsonUtils.result2Json("0"));
            } else if (target.equals("userInfo")) {
                String userId = request.getParameter("userId");
                UserInfoDao dao = new UserInfoDao();
                if (dao.initializeUserInfo(Integer.parseInt(userId))) {
                    response.getWriter().print(JsonUtils.result2Json("1"));
                } else response.getWriter().print(JsonUtils.result2Json("0"));
            } else if (target.equals("shop")) {
                String shopId = request.getParameter("shopId");
                ShopDao dao = new ShopDao();
                boolean result = false;
                try {
                    result = dao.deepDeleteShopById(Integer.parseInt(shopId));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (result) {
                    response.getWriter().print(JsonUtils.result2Json("1"));
                } else response.getWriter().print(JsonUtils.result2Json("0"));
            } else if (target.equals("book")) {
                String bookId = request.getParameter("bookId");
                String shopId=request.getParameter("shopId");
                BookDao dao = new BookDao();
                boolean result=false;
                try {
                    result=dao.deleteBookById(Integer.parseInt(bookId),Integer.parseInt(shopId));
                }catch (SQLException e){
                    e.printStackTrace();
                }
                if (result) {
                    response.getWriter().print(JsonUtils.result2Json("1"));
                } else response.getWriter().print(JsonUtils.result2Json("0"));
            } else response.getWriter().print(JsonUtils.result2Json("0"));
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        SetCode.setEncoding(request,response);
//        AdminService adminService = new AdminServiceImpl();
//        List<Admin> list = adminService.getAllAadmin();
//        request.setAttribute("list",list);
//        request.getRequestDispatcher("/admin/AdminList.jsp").forward(request,response);
        doPost(request,response);
    }
}
